package bupt.party.online.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateTemplate;

import bupt.party.online.dao.IBranchNewsDao;
import bupt.party.online.model.BranchNews;
import bupt.party.online.model.PartyNews;

public class BranchNewsDaoImpl implements IBranchNewsDao {
	private HibernateTemplate hibernateTemplate;
	
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public BranchNews findById(int id) {
		// 根据id查找支部新闻
		String queryString = "from BranchNews a where a.id="+id;
		List<BranchNews> list = hibernateTemplate.find(queryString);
		if(list.size()==0){
			return null;
		}else{
			return list.get(0);
		}
	}

	public BranchNews queryByTitle(String branchNewsTitle) {
		// 根据标题查找支部新闻
		String queryString = "from BranchNews a where a.title="+branchNewsTitle;
		List<BranchNews> list =hibernateTemplate.find(queryString);
		if(list.size()==0){
			return null;
		}else{
			return list.get(0);
		}
	}

	public void deleteBranchNews(BranchNews branchNews) {
		// 删除支部新闻
		hibernateTemplate.delete(branchNews);
	}

	public void saveBranchNews(BranchNews brachNews) {
		// 保存支部新闻
		hibernateTemplate.save(brachNews);

	}

	public void updateBranchNews(BranchNews branchNews) {
		// 删除支部新闻
		hibernateTemplate.update(branchNews);
	}

	public List<BranchNews> queryAll() {
		// 查找所有支部新闻
		return hibernateTemplate.find("from BranchNews");
	}

	public int getResultCountByTag(int tag) {
		// TODO Auto-generated method stub
		if(tag <= 0) return 0;
		
		int count = 0;
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		
		try{
			String sql = "select count(*) from BranchNews b where b.tag=?";
			
			Query query = session.createQuery(sql);
			query.setParameter(0, tag);
			
			count = ((Long)query.uniqueResult()).intValue();
			
			return count;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
		
	}

	public List<BranchNews> getResultsByPage(int tag, int firstResult,
			int pageSize) {
		// TODO Auto-generated method stub
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		try{
			
			if(tag <= 0) return null;
			
			String sql = "from BranchNews b where b.tag=? order by posterTime desc";
			Query query = session.createQuery(sql);
			query.setParameter(0, tag);
			query.setFirstResult(firstResult);
			query.setMaxResults(pageSize);
			
			return query.list();
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	public List<BranchNews> QueryByConditions(String queryString,
			Object[] values, int firstResult, int pageSize) {
		// TODO Auto-generated method stub
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		try{
			
			queryString += " order by posterTime desc";
			
			Query q = session.createQuery(queryString);
			
			if(values != null){
				for(int i=0;i<values.length;i++){
					q.setParameter(i, values[i]);
				}
			}
			
			q.setFirstResult(firstResult);
			
			q.setMaxResults(pageSize);
			
			return q.list();
			
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public int getResultCount(String countSql, Object[] queryValues) {
		// TODO Auto-generated method stub
		int count = 0;
		Session session = this.hibernateTemplate.getSessionFactory().getCurrentSession();
		
		try{
			
			Query query = session.createQuery(countSql);
			
			if(queryValues != null){
				for(int i=0;i<queryValues.length;i++){
					query.setParameter(i, queryValues[i]);
				}
			}
			
			count = ((Long)query.uniqueResult()).intValue();
			
			return count;
			
		}catch(Exception e)
		{
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public boolean saveOrUpdate(BranchNews bean) {
		// TODO Auto-generated method stub
		
		try{
			this.hibernateTemplate.saveOrUpdate(bean);
			return true;
		}catch(Exception e){
			
			e.printStackTrace();
			return false;
			
		}
		
	}

	@Override
	public boolean deleteNewsById(int id) {
		// TODO Auto-generated method stub
		if(id < 0) return false;
		
		try{
			BranchNews newsBean = hibernateTemplate.get(BranchNews.class, id);
			if(newsBean == null) return false;
			hibernateTemplate.delete(newsBean);
			return true;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		
	}

}
